package com.yiruantong.common.core.utils.ip;

import cn.hutool.core.net.NetUtil;
import cn.hutool.http.HtmlUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.yiruantong.common.core.utils.StringUtils;

/**
 * 获取地址类
 *
 * @author YiRuanTong
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AddressUtils {

  // 未知地址
  public static final String UNKNOWN = "XX XX";

  public static String getRealAddressByIP(String ip) {
    if (StringUtils.isBlank(ip)) {
      return UNKNOWN;
    }
    // 内网不查询
    ip = "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip);
    if (NetUtil.isInnerIP(ip)) {
      return "内网IP";
    }
    return RegionUtils.getCityInfo(ip);
  }
}
